// pages/login/login.js
import { lgRequest } from '../../utils/ajax'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    account:'',
    password:'',
    checked:false,
    focustPasswordItem:false,
    togglexiangmu:{},
    togglexiangmu1:{},
    isShow:false,
    delectImgTag: "https://www.mingchuliangzao.com.cn:8632/profile/upload/2022/07/19/713a49f43240fae4d44090d92d7f9cf6.png",
    isMimaShow:false,
    passwordOne:'',
    code:'',
    phone:'',
    shoujihao:'',
  },
  tanchu(){
    this.setData({
      isShow : true
    })
  },
// 打开密码重置
toggleMM(){
  this.setData({
    isMimaShow:true
  })
},
// 关闭密码重置
onClose(){
  this.setData({
    isMimaShow:false
  })
},
// 手机密码和验证码
password(e){
  // console.log(e.detail.value);
  this.setData({
    passwordOne:e.detail.value
  })
},
Code(e){
  this.setData({
    code:e.detail.value
  })
},
phone(e){
  this.setData({
    phone:e.detail.value
  })
  console.log(this.data);
},
// 重置密码
chongzhi(){
  
  wx.request({
    url: `https://www.mengbaoonline.com:8182/system/user/appLogin`,
    data:{
       phonenumber : this.data.phone,
       password : this.data.passwordOne,
       code : this.data.code
    },
    method: 'put',
    dataType: 'json',
    header: {
      'content-type': 'application/json',
      // 'Authorization': wx.getStorageSync('token'),
      // 'headerUserId': user.id,
      // 'headerUserToken': user.userToken
    },
    success: (res) => {
      console.log(res,'res');
      wx.showToast({
              title: res.data.msg,
            })
            this.setData({
              isMimaShow:false
            })
    },
  });
},
// 获取验证码
getCode(){
  let phone=this.data.phone
  console.log(phone);

  wx.request({
    url: `https://www.mengbaoonline.com:8182/duanxin/fasong/send2?phone=${phone}`,
    method: 'GET',
    dataType: 'json',
    header: {
      'content-type': 'application/json',
      // 'Authorization': wx.getStorageSync('token'),
      // 'headerUserId': user.id,
      // 'headerUserToken': user.userToken
    },
    success: (res) => {
      console.log(res);
      wx.showToast({
              title: '获取成功',
            })
    },
  });
},
  onChange() {
    // debugger
    this.setData({
      checked: !this.data.checked,
    });
  },
  bindAccount(e){
    this.setData({
      account: e.detail.value
    })
  },
  focusAccount(e){
    this.setData({
      account: e.detail.value
    })
  },
  focusPassword(e){
    this.setData({
      password: e.detail.value
    })
  },
    // 切换项目
    // togglexiangmu() {
    //   lgRequest('togglexiangmu', {}, {
    //     doSuccess: r => {
    //       console.log(r, '切换项目')
    //       this.setData({
    //         togglexiangmu: r.data
    //       })
    //     }
    //   })
    //   // console.log(111);
    // },
        // 切换项目
        togglexiangmu1() {
          lgRequest('togglexiangmu1', {}, {
            doSuccess: r => {
              console.log(r, '切换项目1')
              this.setData({
                togglexiangmu1: r.data
              })
            }
          })
          // console.log(111);
        },
        closeXiangmuxuanze(){
          this.setData({
            isShow:false
          })
        },
  login(e){
    // console.log(e.currentTarget.dataset.valuename,'e');
    if (!this.data.account || !this.data.password){
      return
    }
    wx.showLoading({
      title: '登录中...',
    })
    let params = {
      username: this.data.account,
      password: this.data.password,
      project:e.currentTarget.dataset.valuename
    }
    console.log(params,'params');
    lgRequest('login',params, {
      errAlert: false,
      doSuccess: r => {
        if (r.code == 200) {
          wx.setStorageSync('token', r.token)
          let params = {
            token: wx.getStorageSync('token')
          }
          lgRequest('useinto', params, {
            errAlert: false,
            doSuccess: r => {
              if (r.code == 200) {
                console.log(r,'useinfo');
                wx.setStorageSync('userInfo', r.data.user)
                this.setData({
                  isShow:false
                })
              } else {
                wx.showToast({
                  title: r.msg,
                  icon: 'none'
                })
              }
            },
            doComplete: r => {
            }
          })
          let loginIngo = {
            account: this.data.account,
            password:this.data.password,
            checked: this.data.checked
          }
          wx.setStorageSync('loginIngo', JSON.stringify(loginIngo))
          wx.showToast({
            title: '登陆成功',
            type:'success'
          })
          wx.switchTab({
            url: '../home/home',
          })
        } else {
          wx.showToast({
            title: r.msg,
            icon: 'none'
          })
        }
      },
      doComplete: r => {
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(111);
    // this.togglexiangmu()
    const loginIngo =   wx.getStorageSync('loginIngo')
    console.log(loginIngo,'loginIngo');
    this.togglexiangmu1()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // console.log(222);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // console.log(333);
    let loginIngo = wx.getStorageSync('loginIngo')
    if(!loginIngo){
      return
    }
    let goodsInfo = JSON.parse(loginIngo)
    if (Object.keys(goodsInfo).length>0){
      this.setData({
        account: goodsInfo.account,
        password: goodsInfo.password,
        checked: goodsInfo.checked
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
})